package com.tamartech.greendb.importers;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.Ostermiller.util.CSVParser;
import com.Ostermiller.util.LabeledCSVParser;
import com.tamartech.greendb.loader.ContactLoader;

// class to initially import Jonathan's spreadsheet. Unlikely ever to need it again
public class JESpreadsheet_Contact
{
    private static Logger logger = Logger.getLogger (JESpreadsheet_Contact.class);
    private static SimpleDateFormat formatDate = new SimpleDateFormat ("dd/MM/yyyy");
        
    public static void main(String[] args) throws Exception
	{
        File inFile = new File (args[0]);
        String wardName = args[1];
        Date rollDate = formatDate.parse(args[2]);
        Date contactDate = formatDate.parse(args[3]);
        String fieldSuffix = args[4]; // 7 or 8
        
        ContactLoader loader = new ContactLoader();
        loader.begin(wardName, rollDate);
        
        LabeledCSVParser csv = new LabeledCSVParser (new CSVParser (new FileInputStream (inFile)));
        
        
        for (;;)
        {
            // not all cols are labelled
            String[] line = csv.getLine();

            if (line == null)
                break;
            
            if (csv.getValueByLabel("U").trim().equals("U"))
                // more headings - ignore
                continue;
            
            String ref1 = csv.getValueByLabel("code").trim();
            String ref2 = csv.getValueByLabel("code 2").trim();
            
            String party = csv.getValueByLabel("0" + fieldSuffix + " Party").trim();
//            String party2008 = csv.getValueByLabel("08 Party").trim();
            if (party.length() == 0)
                party = null;
            
            String notes = csv.getValueByLabel("0" + fieldSuffix + " Issues").trim();
//            String notes2008 = csv.getValueByLabel("08 Issues").trim();
            if (notes.length() == 0)
                notes = null;
            
            if (party != null || notes != null)
                loader.putContact(ref1, ref2, contactDate, party, notes);
        }
        
        csv.close();

        loader.end();
	}	
    
    private static String parseVotedField (String s)
    {
        String result = null;
        
        s = s.trim();
        
        if (s.equalsIgnoreCase("V"))
            result = "Voted";
        else
        if (s.equalsIgnoreCase("P"))
            result = "Postal";
        else
        if (s.equalsIgnoreCase("0"))
            result = "No";
        else
        if (s.length() > 0)
        {
            logger.error("Invalid Voted value: " + s);
        }
        
        return result;
    }
    
    
} 